#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;

long long calculateScore(string s) {
    unordered_map<char, vector<int>> m;
    long long res = 0;
    for (int i = 0; i < s.size(); i++)
    {
        char mirror = (25 - (s[i] - 'a')) + 'a';
        if (m[mirror].size())
        {
            res += (i - m[mirror].back());
            m[mirror].pop_back();
        }
        else m[s[i]].push_back(i);
    }
    return res;
}